﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Devices.Sensors;

namespace TooMuchToDo.MiniGames
{
    public class balancing : MiniGame
    {
        private Texture2D _circle;
        private SpriteFont _fonty;

        public Interpilation rightMiniGameInterpilation = new Interpilation(1);

        //acceleration and velocity
        private Vector2 _location = new Vector2(400, 480 - 50);
        private float _velocity = 0;
        private float _acceleration = 0;

        private Random _rand = new Random();

        private Camera _camera = new Camera(Vector2.Zero, new Vector2(1, 1));

        private Motion _motion = new Motion();
        public override void Load(ContentManager Content)
        {
            _fonty = Content.Load<SpriteFont>("font");
            _motion.Start();
            _circle = Content.Load<Texture2D>("circle");
 	         base.Load(Content);
        }

        public override void Update()
        {
            _camera.Size = new Vector2(rightMiniGameInterpilation.getValue(),1);

            rightMiniGameInterpilation.Update(0.001f);
           
            _acceleration = (_motion.CurrentValue.Attitude.Pitch /15)*-1;
            if (_rand.Next(0, 1000) > 900)
            {
                _acceleration += (float)((_rand.NextDouble() * .4) - .2);
            }
            _velocity += _acceleration;
            _location.X += _velocity;
            base.Update();
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend,SamplerState.LinearClamp,DepthStencilState.Default,RasterizerState.CullNone,null,_camera.getMatrix());
            spriteBatch.DrawString(_fonty, (_motion.CurrentValue.Attitude.Pitch).ToString(), Vector2.Zero, Color.Black);
            SharedGraphicsDeviceManager.Current.GraphicsDevice.ScissorRectangle = RectangleRestraint;
            spriteBatch.Draw(_circle, _location,null,Color.Blue,0.0f,new Vector2(50,50),1.0f,SpriteEffects.None,0);
            spriteBatch.End();
            base.Draw(spriteBatch);
        }
    }
}
